- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path753. Cracking the Safe.go
39 lines (35 loc) · 838 Bytes
/
753. Cracking the Safe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package leetcode
import"math"
constnumber="0123456789"
funccrackSafe(nint, kint) string {
ifn==1 {
returnnumber[:k]
}
visit, total:=map[string]bool{}, int(math.Pow(float64(k), float64(n)))
str:=make([]byte, 0, total+n-1)
fori:=1; i!=n; i++ {
str=append(str, '0')
}
dfsCrackSafe(total, n, k, &str, &visit)
returnstring(str)
}
funcdfsCrackSafe(depth, n, kint, str*[]byte, visit*map[string]bool) bool {
ifdepth==0 {
returntrue
}
fori:=0; i!=k; i++ {
*str=append(*str, byte('0'+i))
cur:=string((*str)[len(*str)-n:])
if_, ok:= (*visit)[cur]; ok!=true {
(*visit)[cur] =true
ifdfsCrackSafe(depth-1, n, k, str, visit) {
// 只有这里不需要删除
returntrue
}
delete(*visit, cur)
}
// 删除
*str= (*str)[0 : len(*str)-1]
}
returnfalse
}